package cn.szzsi.service;


import cn.szzsi.common.model.*;
import cn.szzsi.core.kit.UtilDate;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计
 * Created by yuanzheng on 2016/12/20.
 */
public class BusinessDataStatisticsService{
    public static final BusinessDataStatisticsService bdss = new BusinessDataStatisticsService();

    /**
     * 交易额,订单数
     * @param uid
     * @return
     */
    public Map<String,Object> getBaseData(String uid){
        Map<String,Object> map=new HashMap<String,Object>();
        String lastMonthFirstDayStr= UtilDate.getLastMonthFirstDayStr();
        String monthFirstDayStr= UtilDate.getMonthFirstDayStr();//当月1号日期字符串
        String nowStr= UtilDate.nowDateStr();//当前日期字符串
        Orders orders=new Orders();
        //成交总金额
        //总交易金额
        orders=orders.findFirst("select sum(totalAmount) totalAmount from mall_orders where paymentStatus='2' and uid=?",uid);
        BigDecimal allTotalAmount=orders.getBigDecimal("totalAmount");
        if(allTotalAmount==null || "0".equals(allTotalAmount.toString())){
            allTotalAmount= new BigDecimal(0);
        }
        map.put("allTotalAmount",allTotalAmount);//与上个月的百分比
        //当月交易金额
        orders=orders.findFirst("select sum(totalAmount) totalAmount from mall_orders where paymentStatus='2' and uid=? and createDate>? and createDate<=?",uid,monthFirstDayStr,nowStr);
        BigDecimal totalAmount=orders.getBigDecimal("totalAmount");
        if(totalAmount==null){
            totalAmount=new BigDecimal(0);
        }
        map.put("totalAmount",totalAmount);//当月交易额
        //成交金额相对上个月百分比
        DecimalFormat df = new DecimalFormat("#.00%");
//        String moneyAddPercent;
//        if(lasTotalAmount.compareTo(new BigDecimal(0))<=0){//如果上个月交易金额是0,这个月不是0, 百分比是100%
//            moneyAddPercent="100%";
//        }else{
//            moneyAddPercent=df.format((totalAmount.subtract(lasTotalAmount)).divide(lasTotalAmount,2,ROUND_HALF_DOWN));
//        }

        //完成订单数
        int paid= Orders.PaymentStatus.paid.ordinal();
        //上月完成订单数
        orders=orders.findFirst("select count(*) totalOrders from mall_orders where uid=? and paymentStatus=? ",uid,paid);
        Long allTotalOrders=orders.getLong("totalOrders");
        //当月完成订单数
        orders=orders.findFirst("select count(*) totalOrders from mall_orders where uid=? and paymentStatus=? and createDate>? and createDate<=?",uid,paid,monthFirstDayStr,nowStr);
        Long totalOrders=orders.getLong("totalOrders");
        map.put("totalOrders",totalOrders);

        //成交订单数相对于上个月的百分比
//        String ordersAddPercent;
//        if(lastTotalOrders==0){
//            ordersAddPercent="100%";
//        }else{
//            ordersAddPercent=df.format(MathUtil.div((MathUtil.sub(totalOrders,lastTotalOrders)),lastTotalOrders,4));
//        }
        map.put("allTotalOrders",allTotalOrders);
        map.put("code",0);
        return map;
    }

    /**
     * 会员数,商品数
     * @param uid
     * @return
     */

    public Map<String ,Object> getMemberAndProduct(String uid){
        Map<String,Object> map=new HashMap<String,Object>();
        //会员数
        Member member=new Member();
        member= Member.dao.findFirst("select count(*) members from mall_member where shopId=?",uid);
        Long members=member.getLong("members");
        map.put("members",members);

        //商品数
        Product product=new Product();
        product=Product.dao.findFirst("select count(*)products from mall_product where uid=?",uid);
        Long products=product.getLong("products");
        map.put("products",products);
        map.put("code",0);
        return map;

    }

    /**
     * 用户反馈
     * @param uid
     * @return
     */
    public Map<String,Object> getFeedBack(String uid){
        Map<String,Object> map=new HashMap<String,Object>();
        List<UserFeedback> list= UserFeedback.dao.find("select * from mall_user_feedback where uid=? order by createDate desc limit 15",uid);
        map.put("list",list);
        map.put("size",list.size());
        return map;
    }

    //最新注册用户
    public Map<String ,Object> getNewUser(String uid){
        Map<String,Object> map=new HashMap<String,Object>();
        List<Member> list= Member.dao.find("select username,sex,createDate,headimgurl from mall_member where shopId=? order by createDate desc limit 6",uid);
        map.put("list",list);
        map.put("size",list.size());
        return map;
    }

    //库存预警
    public Map<String,Object> getStoreWarning(String uid){
        Map<String,Object> map=new HashMap<String,Object>();
        //获取商户商品预警值
        SiteConfig siteConfig= SiteConfig.getByShopId(uid);
        int storeWarning=siteConfig.getStoreWarning();//库存预警值
        List<Product> list=Product.dao.find("select name,(store-freezeStore)realstore,productSn,specificationvalues from mall_product where uid=? and (store-freezeStore)<=? order by (store-freezeStore) asc limit 6",uid,storeWarning);
        for(Product p:list){
            p.initParams();
        }
        map.put("list",list);
        map.put("size",list.size());
        return map;

    }
;
}



















